package com.weipeep.sys.controller;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.weipeep.common.config.ApiUrlContant;
import com.weipeep.common.utils.ImageUtils;
import com.weipeep.common.utils.MatrixToImageWriter;
import org.apache.commons.lang3.StringUtils;
import org.krysalis.barcode4j.HumanReadablePlacement;
import org.krysalis.barcode4j.impl.code39.Code39Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

@Controller
public class GenerateController {
    @RequestMapping("/generate/valicode") //对应/user/valicode.do请求
    public void valicode(HttpServletResponse response, HttpSession session) throws Exception{
        //利用图片工具生成图片
        //第一个参数是生成的验证码，第二个参数是生成的图片
        Object[] objs = ImageUtils.createImage();
        //将验证码存入Session
        session.setAttribute("imageCode",objs[0]);
        //将图片输出给浏览器
        BufferedImage image = (BufferedImage) objs[1];
        response.setContentType("image/png");
        OutputStream os = response.getOutputStream();
        ImageIO.write(image, "png", os);
    }

    /***
     * 二维码
     * @param content
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = ApiUrlContant.GENERATOR_QRCODE, method = RequestMethod.GET)
    public void generateQRcode(String content,HttpServletResponse response) throws Exception{
        //校验参数
        if(StringUtils.isBlank(content)){
            content = "0000";   //错误参数，生成“0000”图片。
        }
        response.setContentType("image/png");
        OutputStream out = new BufferedOutputStream(response.getOutputStream());
        try{
            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();

            Map<EncodeHintType, Object> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            hints.put(EncodeHintType.MARGIN, 1);
            BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 150, 150,hints);
            MatrixToImageWriter.writeToStream(bitMatrix, "png", out);
        } finally {
            out.close();
        }
    }
    /***
     * 条形码
     * @param taskId
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = ApiUrlContant.GENERATOR_BARCODE, method = RequestMethod.GET)
    public void generateBarcode(String taskId,HttpServletResponse response) throws Exception{
        //Create the barcode bean
        Code39Bean bean = new Code39Bean();
        final int dpi = 140;
        //Configure the barcode generator
        bean.setModuleWidth(UnitConv.in2mm(2f / dpi)); //makes the narrow bar
        //width exactly one pixel
        bean.setWideFactor(1.5);
        bean.doQuietZone(false);
        bean.setMsgPosition(HumanReadablePlacement.HRP_NONE);
        response.setContentType("image/png");
        OutputStream out = new BufferedOutputStream(response.getOutputStream());
        try {
            //Set up the canvas provider for monochrome JPEG output
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(
                    out, "image/png", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 90);

            //Generate the barcode
            bean.generateBarcode(canvas, taskId);

            //Signal end of generation
            canvas.finish();
        } finally {
            out.close();
        }
    }
}
